package cn.piaoyi.web.action;

import java.util.List;
import java.util.Map;

import cn.piaoyi.bean.Cart;
import cn.piaoyi.bean.Category;
import cn.piaoyi.bean.City;
import cn.piaoyi.bean.Goods;
import cn.piaoyi.bean.Price;
import cn.piaoyi.comm.Util;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.CartService;
import cn.piaoyi.service.PriceService;
import cn.piaoyi.web.interceptor.UserSessionAware;
import cn.piaoyi.web.servlet.UserSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

public class ShopCart extends BaseAction implements SessionAware,UserSessionAware{
	private static final Log logger = LogFactory.getLog(ShopCart.class);
	
	private CartService cartService;

	public CartService getCartService() {
		return cartService;
	}

	public void setCartService(CartService cartService) {
		this.cartService = cartService;
	}
	
	private UserSession userSession;
	private Map session;

	public UserSession getUserSession() {
		return userSession;
	}

	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}
	
	public Map getSession() {
		return session;
	}
	
	public void setSession(Map session) {
		this.session = session;
	}
	
	private List carts;
	private int goodsAmount = 0 ;
	private long id;
	private String ids;

	public List getCarts() {
		return carts;
	}

	public void setCarts(List carts) {
		this.carts = carts;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public int getGoodsAmount() {
		return goodsAmount;
	}

	public void setGoodsAmount(int goodsAmount) {
		this.goodsAmount = goodsAmount;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		this.setCarts(this.getCartService().findCarts(getUserSession().getUid(), "id", 0));
		if(this.carts == null || this.carts.isEmpty()){
			goodsAmount =0;
		}else{
			for(int i=0;i<this.carts.size();i++){
				Cart c = (Cart) carts.get(i);
				goodsAmount+=c.getTotalPrice();
			}
		}
		return SUCCESS;
	}
	
	public String dodel(){
		this.getCartService().removeCartById(this.id);
		return "index";
	}
	
	public String dodels(){
		this.getCartService().removeCartInIds(Util.string2List(ids));
		return "index";
	}
}
